import { MockMethod } from 'vite-plugin-mock'

let products = [
  {
    id: 1,
    name: '商品1',
    category: '电子产品',
    price: 999.99,
    stock: 100,
    status: '在售',
    createTime: '2024-01-01 12:00:00'
  },
  {
    id: 2,
    name: '商品2',
    category: '服装',
    price: 199.99,
    stock: 200,
    status: '在售',
    createTime: '2024-01-02 12:00:00'
  }
]

export default [
  {
    url: '/api/products',
    method: 'get',
    response: () => ({
      code: 200,
      data: products,
      message: 'success'
    })
  },
  {
    url: '/api/product',
    method: 'post',
    response: ({ body }) => {
      const newProduct = {
        ...body,
        id: products.length + 1,
        createTime: new Date().toLocaleString()
      }
      products.push(newProduct)
      return {
        code: 200,
        data: newProduct,
        message: 'success'
      }
    }
  },
  {
    url: '/product/:id',
    method: 'put',
    response: ({ body }) => {
      const index = products.findIndex(item => item.id === body.id)
      if (index > -1) {
        products[index] = { ...products[index], ...body }
        return {
          code: 200,
          data: products[index],
          message: 'success'
        }
      }
      return {
        code: 400,
        message: '商品不存在'
      }
    }
  },
  {
    url: '/product/:id',
    method: 'delete',
    response: ({ params }) => {
      const index = products.findIndex(item => item.id === Number(params.id))
      if (index > -1) {
        products.splice(index, 1)
        return {
          code: 200,
          message: 'success'
        }
      }
      return {
        code: 400,
        message: '商品不存在'
      }
    }
  }
] as MockMethod[] 